package com.aboo.util;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


/**
 * @author ���� Created on 2005-9-17
 */
public class HibernateSessionFactory {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(HibernateSessionFactory.class);

	private static String CONFIG_FILE_LOCATION = "hibernate.cfg.xml";

	private static final ThreadLocal threadLocal = new ThreadLocal();

	private static final ThreadLocal threadTransaction = new ThreadLocal();

	private static SessionFactory sessionFactory;

	private static HibernateSessionFactory instance = new HibernateSessionFactory();

	public static Session currentSession() throws HibernateException {
		Session session = (Session) threadLocal.get();
		logger.debug("get the session from threadLocal.." + session);
		if (session == null) {
			session = sessionFactory.openSession();
			threadLocal.set(session);
		}
		return session;
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static void closeSession() throws HibernateException {
		Session session = (Session) threadLocal.get();
		logger.debug("the session in the threadlocal is :: " + session);
		threadLocal.set(null);
		if (session != null) {
			session.close();
		}
	}

	private HibernateSessionFactory() {
		try {

			logger.debug("loading hibernate session factory");
			/*CONFIG_FILE_LOCATION�ļ��ͱ��ļ���ͬһ��Ŀ¼��
			sessionFactory = new Configuration().configure(
					getClass().getResource(CONFIG_FILE_LOCATION))
					.buildSessionFactory();
			*/
			/*CONFIG_FILE_LOCATION�ļ������ϲ�Ŀ¼��
			sessionFactory = new Configuration().configure()
					.buildSessionFactory();
			*/
			sessionFactory = new Configuration().configure()
					.buildSessionFactory();
		} catch (Exception e) {
			System.err.println("Error Creating SessionFactory");
			e.printStackTrace();
		}
	}

	// ********************************************************************* //
	/**
	 * Start a new database transaction.
	 */
	public static void beginTransaction() throws HibernateException {
		Transaction tx = (Transaction) threadTransaction.get();
		if (tx == null) {
			logger.debug("Starting new database transaction in this thread.");
			tx = currentSession().beginTransaction();
			threadTransaction.set(tx);
		}
	}

	/**
	 * Commit the database transaction.
	 */
	public static void commitTransaction() throws HibernateException {
		Transaction tx = (Transaction) threadTransaction.get();
		if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
			logger.debug("Committing database transaction of this thread.");
			tx.commit();
		}
		threadTransaction.set(null);
	}

	/**
	 * Commit the database transaction.
	 */
	public static void rollbackTransaction() throws HibernateException {
		Transaction tx = (Transaction) threadTransaction.get();
		threadTransaction.set(null);
		if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
			logger.debug("Tyring to rollback database transaction of this thread.");
			tx.rollback();
		}
	}
}